/*
 * @Descripttion: 
 * @version: 
 * @Author: lily
 * @Date: 2021-05-07 14:35:22
 * @LastEditors: lily
 * @LastEditTime: 2021-05-07 14:58:38
 */
/*
 * @lc app=leetcode.cn id=283 lang=javascript
 *
 * [283] 移动零
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */

//  思想：双指针 
//  将0与后面第一个非0的数交换

//  复杂度：O(n) O(1)

var moveZeroes = function (nums) {
    let i = 0
    for (let j = 0; j < nums.length; j++) {
        // j指向非0数
        if (nums[j] !== 0) {
            [nums[i], nums[j]] = [nums[j], nums[i]]
            i++
        }
    }

};
// @lc code=end

console.log(moveZeroes([1, 0, 1]));